package com.neusoft.elmboot.common;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.neusoft.elmboot.common.result.Result;
import org.springframework.http.MediaType;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class AutheAuthoResp {


    public static void accessDeniedResponse(HttpServletRequest request,HttpServletResponse response) {
        response.setContentType(MediaType.APPLICATION_JSON_VALUE); // 设置响应内容类型为JSON
        response.setCharacterEncoding("UTF-8");
        Result result = Result.error( "权限不够，服务器资源:[" + request.getRequestURI() + "]拒绝访问！");

        ObjectMapper objectMapper = new ObjectMapper();
        String jsonBody = null;
        try {
            jsonBody = objectMapper.writeValueAsString(result);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }

        PrintWriter writer = null;
        try {
            writer = response.getWriter();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        writer.write(jsonBody);
        writer.flush();
    }

}
